package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.time.LocalDate;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private IUserService service;

    @RequestMapping("findByItem")
    public IPage<UserBean> findByItem(Integer pageNO, String username,
                                      LocalDate startDate, LocalDate endDate) {
        return service.findByItem(pageNO, username, startDate, endDate);

    }

    @RequestMapping("findById")
    public UserBean findById(Integer id) {
      return   service.findById(id);
    }
    @RequestMapping("update")
    public String update(Integer id,Integer salary){
        service.update(id,salary);
        return "ok";
    }
    @RequestMapping("add")
    public String add(@Valid UserBean user, BindingResult result) throws Exception{

        if (result.hasErrors()){
            ObjectMapper om = new ObjectMapper();
           return om.writeValueAsString(result.getFieldErrors());
        }

       service.add(user);
       return "ok";
    }
    @RequestMapping("del")
    public String del(Integer id){
        service.del(id);
        return "ok";
    }


}
